﻿using System.IO;

class FileEncryption
{
    private const string INPUT_FILE_NAME = @"..\..\..\FileEncryption\example.bin";

    private static void Encrypt(FileStream output, byte[] data)
    {
        output.Write(data, data.Length - 1, 1);
        output.Write(data, 0, data.Length - 1);
    }

    public static void Main()
    {
        FileStream file = new FileStream(INPUT_FILE_NAME, FileMode.Open, FileAccess.ReadWrite);
        using (file)
        {
            //string tempFile = Path.GetTempFileName();
            while (file.Position < file.Length)
            {
                if (file.Length - file.Position >= 5)
                {
                    ProcessPart(file,5);
                }
                else
                {
                    ProcessPart(file,file.Length - file.Position);
                }
            }

        }
    }

    private static void ProcessPart(FileStream file,long size)
    {
        byte[] data = new byte[size];
        file.Read(data, 0, data.Length);
        file.Position = file.Seek(-size, SeekOrigin.Current);
        Encrypt(file, data);
    }
}
